package a;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.Set;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

import panel.CommandPanel;
import Files.File_;
import a.SwingShell.TerminalDialog.RemoteConsumer;
import ch.ethz.ssh2.Session;
import exc.MyException;

public class _TerminalDialog extends JTabbedPane
{
	private static final long serialVersionUID = 1L;

	JPanel botPanel;
	JButton logoffButton;
	JTextArea terminalArea;

	Session sess;
	InputStream in;
	OutputStream out;

	int x, y;
	class RemoteConsumer extends Thread
	{
		char[][] lines = new char[y][];
		int posy = 0;
		int posx = 0;

		private void addText(byte[] data, int len)
		{
			for (int i = 0; i < len; i++)
			{
				char c = (char) (data[i] & 0xff);
				if (c == 8) // Backspace, VERASE
				{
					if (posx < 0)
						continue;
					posx--;
					continue;
				}
				if (c == '\r')
				{
					posx = 0;
					continue;
				}
				if (c == '\n')
				{
					posy++;
					if (posy >= y)
					{
						for (int k = 1; k < y; k++)
							lines[k - 1] = lines[k];
						posy--;
						lines[y - 1] = new char[x];
						for (int k = 0; k < x; k++)
							lines[y - 1][k] = ' ';
					}
					continue;
				}

				if (c < 32)
				{
					continue;
				}
				if (posx >= x)
				{
					posx = 0;
					posy++;
					if (posy >= y)
					{
						posy--;
						for (int k = 1; k < y; k++)
							lines[k - 1] = lines[k];
						lines[y - 1] = new char[x];
						for (int k = 0; k < x; k++)
							lines[y - 1][k] = ' ';
					}
				}
				if (lines[posy] == null)
				{
					lines[posy] = new char[x];
					for (int k = 0; k < x; k++)
						lines[posy][k] = ' ';
				}
				lines[posy][posx] = c;
				posx++;
			}
			StringBuffer sb = new StringBuffer(x * y);
			for (int i = 0; i < lines.length; i++)
			{
				if (i != 0)
					sb.append('\n');
				if (lines[i] != null)
				{
					sb.append(lines[i]);
				}
			}
			setContent(sb.toString(),aa);
		}
		 
		JTextArea aa;
		public RemoteConsumer(JTextArea pp)
		{
			aa=pp;
			//aa.setPreferredSize(new Dimension(50,50));
		}
		public void run()
		{
			byte[] buff = new byte[8192];
			try
			{
				while (true)
				{
					int len = in.read(buff);
					if (len == -1)
						return;
					addText(buff, len);
				}
			}
			catch (Exception e)
			{
			}
		}
	}
	public _TerminalDialog( String title, Session sess, int x, int y) throws IOException
	{
		//super(parent, title, true);

		this.sess = sess;

		in = sess.getStdout();
		out = sess.getStdin();

		this.x = x;
		this.y = y;
		Font f = new Font("Monospaced", Font.PLAIN, 12);

		terminalArea = new JTextArea(y, x);
		terminalArea.setSize(200, 200);
		terminalArea.setFont(f);
		terminalArea.setBackground(Color.BLACK);
		terminalArea.setForeground(Color.ORANGE);
		terminalArea.setCaretColor(Color.BLACK);
		KeyAdapter kl = new KeyAdapter()
		{
			public void keyTyped(KeyEvent e)
			{
				int c = e.getKeyChar();

				try
				{
					out.write(c);
				}
				catch (IOException e1)
				{
				}
				e.consume();
			}
		};

		terminalArea.addKeyListener(kl);
		botPanel = new JPanel(new BorderLayout());
		
		MouseListener mouseListener = new MouseAdapter() {
		    public void mouseClicked(MouseEvent e) {
		       System.out.println("zzzzzzzzzzzzzzz"+e.getSource());
		       String example = "This is only an example";
		       example="";
		       for(Object key:File_.common_map_commands.keySet())
		       {
		    	   System.out.println(((JList)e.getSource()).getSelectedValue().toString()+" "+key);
		    	   if(
		    			   ((JList)e.getSource()).getSelectedValue().equals(key))//==key)
		    	   {
		    		   example+=File_.common_map_commands.get(key);break;
		    	   }
		    	   
		       }
		       byte[] bytes = example.getBytes();
		       try {
				out.write(bytes);
			} catch (IOException e1) {
				//e1.printStackTrace();
			}
		       /*terminalArea.setText(
		    		   terminalArea.getText()+((JList)e.getSource()).getSelectedValue());*/
		       //
		       //
		       //
		       //obsluga pola z poleceniami
		       //
		       //
		       //
		    }
		};

		GridLayout experimentLayout = new GridLayout(1,0);
		//botPanel.setLayout(experimentLayout);
			JList		listbox=new JList();listbox.setPreferredSize(new Dimension(50,600));
			DefaultListModel df= new DefaultListModel ();
			
			Map<String,String> c= new File_().readF(0);
			if (!c.isEmpty()){
				df.clear();
				 
			Set<String> test = c.keySet();
			for (String key : test) {
				df.addElement(c.get(key));
			    }
			}
			listbox.setModel(df);		listbox.addMouseListener(mouseListener);
		/*botPanel.add(terminalArea,BorderLayout.PAGE_START);
		botPanel.add(listbox,BorderLayout.PAGE_START);
		JButton button = new JButton("5 (LINE_END)9999");
		botPanel.add(button,BorderLayout.PAGE_END);*/
		
		
		JButton button2 = new JButton("Button 1 (PAGE_START)");
	//	botPanel.add(button2, BorderLayout.PAGE_START);
		botPanel.add(terminalArea, BorderLayout.PAGE_START);
		

		//Make the center component big, since that's the
		//typical usage of BorderLayout.
		GridLayout layout = new GridLayout(0,3);
		JPanel d=new JPanel(layout);
	
		JButton button3 = new JButton("Button 2 (CENTER)");
		//botPanel.add(button3, BorderLayout.CENTER);
		botPanel.add(d, BorderLayout.CENTER);
		/*JButton button11 = new JButton("Button 3 (LINE_START)");
		botPanel.add(button11, BorderLayout.LINE_START);

		JButton buttona = new JButton("Long-Named Button 4 (PAGE_END)");
		botPanel.add(buttona, BorderLayout.PAGE_END);

		JButton buttonx = new JButton("5 (LINE_END)");
		botPanel.add(buttonx, BorderLayout.LINE_END);*/
		
		JButton buttonx = new JButton("5 (LINE_END)");
		d.add(buttonx);
		JButton buttonx1 = new JButton("51 (LINE_END)");
		d.add(buttonx1);
		JButton buttonx12 = new JButton("52 (LINE_END)");
		d.add(buttonx12);
		JButton buttonx13 = new JButton("5 3(LINE_END)");
		d.add(buttonx13);
		
		
		SwingShell.arraycomandpanel.put("labtest", new CommandPanel("Test"));
	//	botPanel.add(arraycomandpanel.get("labtest"),BorderLayout.CENTER);
		
		//////////////////////////////////
		SwingShell.tabbedPane.addTab( "Page 12", botPanel );
		SwingShell.loginFrame.add(SwingShell.tabbedPane);
		//////////////////////////////////
		new RemoteConsumer(terminalArea).start();
		//new RemoteConsumer(terminalArea).start();
	}

	public void setContent(String lines,JTextArea pole)
	{
		pole.setText(lines);
		//terminalArea1.setText(lines);
	}
}
